﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kaskelot.SP.Core
{
	public static class UrlUtil
	{
		private static string[] ForbiddenFolderEndings = new string[]
		{
			".files",
			"_files",
			"-Dateien",
			"_fichiers",
			"_bestanden",
			"_file",
			"_archivos",
			"-filer",
			"_tiedostot",
			"_pliki",
			"_soubory",
			"_elemei",
			"_ficheiros",
			"_arquivos",
			"_dosyalar",
			"_datoteke",
			"_fitxers",
			"_failid",
			"_fails",
			"_bylos",
			"_fajlovi",
			"_fitxategiak"
		};

		public static string CleanUrl(string orgUrl)
		{
			char[] charToTrim = { '/', '&', '~', '#', '%', '*', '{', '}', '\\', ':', '<', '>', '?', '+', '|', '"', '$', '\'', '^', '¨', '´', '`', '@', '[', ']', '=', ';', ',', '£', '!', '(', ')', '¤', '€' };

			char[] charToTrimStart = { '_', '.' };
			char[] charToTrimEnd = { '.' };

			for (int i = 0; i < charToTrim.Length; i++)
			{
				orgUrl = orgUrl.Replace(charToTrim.ElementAt(i).ToString(), "");
			}

			orgUrl = orgUrl.Replace(" ", "-");

			//Get rid of all consecutively periods
			while (orgUrl.Contains(".."))
				orgUrl = orgUrl.Replace("..", ".");

			while (orgUrl.StartsWith("_") || orgUrl.StartsWith("."))
				orgUrl = orgUrl.TrimStart(charToTrimStart);

			orgUrl = orgUrl.TrimEnd(charToTrimEnd);

			return orgUrl;
		}

		public static string CleanFileName(string filename)
		{
			char[] charToTrim = { '/', '&', '~', '#', '%', '*', '{', '}', '\\', ':', '<', '>', '?', '|', '"', '$', '^', '£' };

			char[] charToTrimStart = { '.' };
			char[] charToTrimEnd = { '.' };

			for (int i = 0; i < charToTrim.Length; i++)
			{
				filename = filename.Replace(charToTrim.ElementAt(i).ToString(), "");
			}

			//Get rid of all consecutively periods
			while (filename.Contains(".."))
				filename = filename.Replace("..", ".");

			while (filename.StartsWith("."))
				filename = filename.TrimStart(charToTrimStart);

			filename = filename.TrimEnd(charToTrimEnd);

			if (ForbiddenFolderEndings.Any(s => filename.ToLower().EndsWith(s)))
				filename += "_";

			return filename;
		}

		public static string CleanGroupName(string groupname)
		{
			char[] charToTrim = { '/', '&', '~', '#', '%', '*', '{', '}', '\\', ':', '<', '>', '?', '+', '|', '"', '$', '\'', '^', '¨', '´', '`', '@', '[', ']', '=', ';', ',', '£' };

			char[] charToTrimStart = { '.' };
			char[] charToTrimEnd = { '.' };

			for (int i = 0; i < charToTrim.Length; i++)
			{
				groupname = groupname.Replace(charToTrim.ElementAt(i).ToString(), "");
			}

			//Get rid of all consecutively periods
			while (groupname.Contains(".."))
				groupname = groupname.Replace("..", ".");

			while (groupname.StartsWith("."))
				groupname = groupname.TrimStart(charToTrimStart);

			groupname = groupname.TrimEnd(charToTrimEnd);

			return groupname;
		}

		public static string EnsureHTTP(this string url)
		{
			if (url == null)
				return null;
			if (url.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase) || url.StartsWith("https://", StringComparison.CurrentCultureIgnoreCase))
				return url;
			else
				return "http://" + url;
		}
	}
}
